<?php
class UserAction extends Action{
    /*******************
	 * 用户登录
	 ******************/
    public function login(){
    	$this->assign('APP_PUBLIC_PATH',APP_PUBLIC_PATH.'/');
    	$this->display('Index:login');
    	
    }
    /*******************
	 * 登录检测
	 ******************/
	public function checkLogin() {
		if(empty($_POST['username'])) {
			$this->error_show("请输入用户名");
		}elseif (empty($_POST['password'])){
			$this->error_show("密码不能为空");
		}elseif($_SESSION['verify'] != md5($_POST['verify'])) {
			$this->error_show('验证码错误！');
		}else{
			$name = M('member');
			$adminuse = $name->where("competence = '5'")->findall();
			if ($_POST['username']!= $adminuse[0]['username']){
				$this->error_show("用户名或密码错误");
			}elseif (md5($_POST['password'])!= $adminuse[0]['password']){
				$this->error_show("用户名或密码错误");
			}else{
				$_SESSION['User'] = $adminuse[0]['username'];
				echo '<SCRIPT LANGUAGE="javascript"> 
window.location.href="'.__APP__.'" 
</SCRIPT> 
';
				
			}
		}
	}
    /*******************
	 * 错误提示页
	 ******************/
	public function error_show($msn,$Url) {
		 echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		 echo '<script type=text/javascript>top.alert("'.$msn.'");</script>';
		 if (empty($Url)) {
		 	$this->TWinBack();
		 }else{
		 	$this->assign("url",$Url);
		 	$this->Go2Url($Url);
		 }
		 
	}
	
    /*******************
	 * 登录检查
	 ******************/
	public function checkuser(){
		if(!isset($_SESSION['User'])) {
			$this->Go2Url('admin.php?m=User&a=login');
		}
	}
    /*******************
	 * 验证码
	 ******************/
	 public function verify()
    {
		$type	 =	 isset($_GET['type'])?$_GET['type']:'gif';
        import("@.ORG.Image");
        Image::buildImageVerify(4,1,$type);
    }
    /*******************
	 * 注销登录
	 ******************/
	public function logout(){
		session_destroy();
		$this->Go2Url('admin.php');
	}
//以下为常用JS功能PHP封装
		protected function walert($msg,$status=true){
			echo '<script type=text/javascript>top.alert("'.$msg.'");</script>';
			$status?exit:"";
		}
		protected function Reload($window="window"){//窗口刷新
			echo '<script type=text/javascript>'.$window.'.location.reload();</script>';
		}
		protected function TWinReload(){
			$this->Reload();
		}
		protected function SelfClose(){//窗口关闭
			echo '<script type=text/javascript>window.close();</script>';
		}
		protected function Go2Url($url,$window='window'){//跳转及窗口的选择
			echo '<script type=text/javascript>'.$window.'.location.href="'.$url.'"</script>';
		}
		protected function TWinBack(){//回上一页
			echo '<script type=text/javascript>top.history.go(-1);</script>';
		}
}
?>